package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Override
    public void saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {
        goodsDao.updateById(goodsId,inventoryQuantity,purchasingPrice);
    }

    @Override
    public void deleteStock(Integer goodsId) {
        goodsDao.updateStock(goodsId);
    }

    @Override
    public Map<String, Object> getGoodsAlarmList() {
        Map<String, Object> map = new HashMap<>();
        List<Goods> goodsAlarmList = goodsDao.selectGoodsAlarmList();
        map.put("rows",goodsAlarmList);
        return map;
    }

    @Override
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        Map<String, Object> map = new HashMap<>();
        List<Goods> hasGoodsList = goodsDao.selectHasGoodSList(nameOrCode);
        Integer hasToatal = goodsDao.selectHasTotal(nameOrCode);
        int firstIndex = (page - 1) * rows;
        int lastIndex = page * rows;
        if (hasToatal<lastIndex){
            lastIndex = hasToatal;
        }
        List<Goods> goodsPage = hasGoodsList.subList(firstIndex, lastIndex);
        map.put("total",hasToatal);
        map.put("rows",goodsPage);
        return map;
    }

    @Override
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        HashMap<String, Object> map = new HashMap<>();
        List<Goods> noGoodsList = goodsDao.selectNoGoodSList(nameOrCode);
        Integer noToatal = goodsDao.selectNoTotal(nameOrCode);
        int firstIndex = (page - 1) * rows;
        int lastIndex = page * rows;
        if (noToatal<lastIndex){
            lastIndex = noToatal;
        }
        List<Goods> goodsPage = noGoodsList.subList(firstIndex, lastIndex);
        map.put("total",noToatal);
        map.put("rows",goodsPage);
        return map;
    }

    @Override
    public Map<String, Object> getGoodsList(Integer page, Integer rows, String goodsName, Integer goodsTypeId) {
        HashMap<String, Object> map = new HashMap<>();
        List<Goods> goodsList = goodsDao.selectGoods(goodsName,goodsTypeId);
        Integer total = goodsDao.selectGoodsTotal(goodsName,goodsTypeId);
        int firstIndex = (page - 1) * rows;
        int lastIndex = page * rows;
        if (total<lastIndex){
            lastIndex = total;
        }
        List<Goods> goodsPage = goodsList.subList(firstIndex, lastIndex);
        map.put("total",total);
        map.put("rows",goodsPage);
        return map;
    }

    @Override
    public Map<String, Object> getListInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
        List<Goods> goodsList = goodsDao.selectGoodsList(codeOrName,goodsTypeId);
        Integer total = goodsDao.selectTotal(codeOrName,goodsTypeId);
        int firstIndex = (page - 1) * rows;
        int lastIndex = page * rows;
        if (total<lastIndex){
            lastIndex = total;
        }
        List<Goods> goodsPage = goodsList.subList(firstIndex, lastIndex);
        Map<String, Object> map = new HashMap<>();
        map.put("rows",goodsPage);
        map.put("total",total);
        return map;
    }
}
